package com.zis.auth.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zis.auth.service.ISercretInter;
import com.zis.core.ResponseEntity;
import com.zis.auth.model.Login;
import com.zis.auth.model.ResetPwd;
import com.zis.auth.service.ILoginInter;
import com.zis.platform.model.User;
import com.zis.platform.service.IUserInter;
import com.zis.utils.MD5Util;
import com.zis.utils.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Create by wangshen 2017/12/21
 */
@Service
public class LoginInterImpl implements ILoginInter {

    @Autowired
    private IUserInter userInter;

    @Autowired
    private ISercretInter sercretInter;

    @Override
    public String login(Login login) throws Exception {
        if(login.getToken()==null||login.getToken().equals("")){
            return ResponseEntity.createErrorJsonResponse("登录信息无效");
        }
        if(login.getUserName()==null||login.getUserName().equals("")){
            return ResponseEntity.createErrorJsonResponse("请输入用户名");
        }
        if(login.getPassword()==null||login.getPassword().equals("")){
            return ResponseEntity.createErrorJsonResponse("请输入密码");
        }
        User params = new User();
        params.setLoginName(login.getUserName());
        List<User> userList = userInter.findByCondition(params);
        if(userList.size()==0){
            return ResponseEntity.createErrorJsonResponse("用户名不存在");
        }
        User user = userList.get(0);
        String salt = user.getSalt();
        System.out.println(JSON.toJSONString(user));
        String patter = SecurityUtil.AESDncode(salt,user.getPassword());
        // 获得系统存储的密码明文进行md5操作
        String password = MD5Util.string2MD5(patter+login.getToken());
        if(password==null||!password.equals(login.getPassword())){
            return ResponseEntity.createErrorJsonResponse("密码错误");
        }
        String client = sercretInter.getClient(login.getToken());
        JSONObject sercretResult = JSONObject.parseObject(client);
        if(!sercretResult.get("code").equals(0)){
            return ResponseEntity.createErrorJsonResponse("登陆信息不存在");
        }
        // 校验通过将用户信息设置到缓存
        JSONObject clientJSON = JSON.parseObject(sercretResult.getString("respData"));
        clientJSON.put("userCode", user.getUserId());
        clientJSON.put("userName", user.getRealName());
        // 将登陆信息更新到redis里面
        String code = sercretInter.updateClientUser(clientJSON);
        if(code.equals("1")){
            return ResponseEntity.createErrorJsonResponse("登陆信息更新失败");
        }
        clientJSON.remove("sign");// 此信息不返回给前端
        return ResponseEntity.createNormalJsonResponse(clientJSON);
    }

    /**
     * 密码重置修改
     * @param resetPwd
     * @return
     * @throws Exception
     */
    @Override
    public String resetPassword(ResetPwd resetPwd) throws Exception {
        User params = new User();
        params.setUserId(resetPwd.getUserId());
        List<User> userList = userInter.findByCondition(params);
        if(userList.size()==0){
            return ResponseEntity.createErrorJsonResponse("未获取到用户信息");
        }
        User user = userList.get(0);
        String salt = user.getSalt();
        String patter = SecurityUtil.AESDncode(salt,user.getPassword());
        if(patter==null||!patter.equals(resetPwd.getOldPwd())){
            return ResponseEntity.createErrorJsonResponse("原密码错误");
        }
        if(resetPwd.getNewPwd()==null||resetPwd.getNewPwd().equals("")){
            return ResponseEntity.createErrorJsonResponse("新密码不能为空");
        }
        if(!resetPwd.getNewPwd().equals(resetPwd.getNewPwd2())){
            return ResponseEntity.createErrorJsonResponse("两次密码不一致");
        }
        String newPwd = SecurityUtil.AESEncode(salt,resetPwd.getNewPwd());
        params = new User();
        params.setUserId(resetPwd.getUserId());
        params.setPassword(newPwd);
        return userInter.update(params);
    }
}
